home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / hypercar / xfcn / spttool.cpt / Support Tools eXternals 1.2.5 / card_13149.txt < prev    next >
Text File  |  1990-11-13  |  8KB  |  260 lines

  1. -- card: 13149 from stack: in.5
  2. -- bmap block id: 13474
  3. -- flags: 0000
  4. -- background id: 3858
  5. -- name: UnmountVolume
  6. ----- HyperTalk script -----
  7. on hideObjects
  8.   hide cd btn "Try It!"
  9. end hideObjects
  10.  
  11. on showObjects
  12.   show cd btn "Try It!"
  13. end showObjects
  14.  
  15.  
  16. -- part 1 (button)
  17. -- low flags: 00
  18. -- high flags: A002
  19. -- rect: left=82 top=185 right=219 bottom=175
  20. -- title width / last selected line: 0
  21. -- icon id / first selected line: 0 / 0
  22. -- text alignment: 1
  23. -- font id: 0
  24. -- text size: 12
  25. -- style flags: 8192
  26. -- line height: 16
  27. -- part name: Try it!
  28. ----- HyperTalk script -----
  29. on mouseUp
  30.   -- umount the specified disk
  31.   global errGlobal
  32.   put volumePath() into newVolume
  33.   if newVolume = empty then exit mouseUp
  34.   UnmountVolume newVolume, "noDialog:errGlobal"
  35.   if errGlobal Γëá empty then
  36.     answer "Error: ΓÇ£" & errGlobal & "ΓÇ¥"
  37.     put empty into errGlobal
  38.   end if
  39. end mouseUp
  40.  
  41.  
  42.  
  43.  
  44. -- part contents for background part 38
  45. ----- text -----
  46. 47/50
  47.  
  48. -- part contents for background part 20
  49. ----- text -----
  50.      An XCMD which Unmounts the specified volume.
  51.  
  52.      Calling syntax : UnmountVolume volName, <"noDialog:"errGlobal>
  53.   VOLNAME: the volume to check.  
  54.  
  55.  
  56. -- part contents for background part 42
  57. ----- text -----
  58.  { UnmountDisk(pathname ┬½,ΓÇ£noDialog:ΓÇ¥errorGlobal┬╗)    }
  59. { XFCN to unmount the specified volume.            }
  60. {}
  61. {  brought to you by:  Anup Murarka      Eric Carlson    }
  62. {            ALINK:  SKEPTIC      ALINK:  cyNic  }
  63. {                  CIS:  76004,3356    }
  64. {}
  65. {        We are part of the Support Tools Development Group,  }
  66. {        Apple Computer, Inc.  }
  67. {}
  68. {        please DO NOT contack Mac DTS for support of this code!  }
  69. {}
  70. {        please DO contact the authors for support of this code!  }
  71. {}
  72. {        Send comments, bug reports, requests to any of the above  }
  73. {        E-mail addresses or to:}
  74. {}
  75. {              (one of us)          }
  76. {              Apple Computer, Inc.    }
  77. {              900 E. Hamilton, Ave.    }
  78. {              Campbell, CA   95008    }
  79. {              M/S 72-L          }
  80. {}
  81. {  Copyright:  ┬⌐ 1989, 1990 by Apple Computer, Inc., all rights reserved.  }
  82. {}
  83. { written by Eric Carlson                    }
  84. { AppleLink:  cyNic                        }
  85. { modification history                                           }
  86. {       Date        Initials                  Comments                  }
  87. {       ----        ------    ---------------------------------------------------}
  88. {    3/1/90        ec      first written                                }
  89. {    6/6/90      ec      added additional comments to code                 }
  90. {}
  91.  
  92. unit UnmountDisk;
  93.  
  94. interface
  95.  
  96.   uses
  97.     HyperXCMD;
  98.  
  99.   procedure MAIN (paramPtr: XCmdPtr);
  100.  
  101. implementation
  102.  
  103.   procedure reportToUser (paramPtr: XCmdPtr;
  104.                   msgStr: str255);
  105. {}
  106. { report something back to the user.  }
  107. { the last parameter (optional) to an external may contain }
  108.  { "noDialog" or "noDialog:GlobalName".  GlobalName is the name }
  109.  { of a HyperTalk global variable into which error messages will be }
  110.  { placed.  we've decided to use this approach to avoid confusing }
  111. { an error message with a valid result being returned from an XFCN. }
  112. {}
  113.     var
  114.       tempStr: str255;
  115.   begin
  116. {check the last param to see if the user requested that}
  117. { we suppress the error dialog }
  118.     ZeroToPas(paramPtr, paramPtr^.params[paramPtr^.paramCount]^, tempStr);
  119.     UprString(tempStr, true);
  120.     if pos('NODIALOG', tempStr) = 0 then
  121.   { no special error handling specified, throw up a dialog and return the error message }
  122.       begin
  123.         SendCardMessage(paramPtr, concat('answer "', msgStr, '"'));
  124.         paramPtr^.returnValue := PasToZero(paramPtr, msgStr);
  125.       end
  126.     else if (pos(':', tempStr) > 0) then
  127.   { requested global AND noDialog so we fill in the global and return empty }
  128.       begin
  129.         tempStr := copy(tempStr, pos(':', tempStr) + 1, length(tempStr));
  130.                             { get the name of the HC global  to fill }
  131.         SetGlobal(paramPtr, tempStr, PasToZero(paramPtr, msgStr));
  132.                             { and fill it }
  133.         paramPtr^.returnValue := PasToZero(paramPtr, '');  { return empty }
  134.       end
  135.     else
  136.   { requested noDialog only so we return the error condition as the result }
  137.       paramPtr^.returnValue := PasToZero(paramPtr, msgStr);
  138.   end;  { procedure }
  139.  
  140.   function AskedForHelp (paramPtr: XCmdPtr;
  141.                   syntaxMsg: Str255;
  142.                   copyrightMsg: Str255): boolean;
  143. {  check to see if the user sent a '?' or a '!' as }
  144. { the only parameter. if so we will respond with }
  145. { the calling syntax or the copyright/version info }
  146. { for this external }
  147. {}
  148.     var
  149.       firstStr: str255;
  150.   begin
  151.     askedForHelp := false;
  152.     if paramPtr^.paramCount = 1 then
  153.       begin
  154.         ZeroToPas(paramPtr, paramPtr^.params[1]^, firstStr);
  155.           { what is the first param? }
  156.         if firstStr = '?' then
  157.           begin
  158.             reportToUser(paramPtr, syntaxMsg);
  159.             askedForHelp := true
  160.           end  { asked for help }
  161.         else if firstStr = '!' then
  162.           begin
  163.             reportToUser(paramPtr, copyRightMsg);
  164.             askedForHelp := true
  165.           end;  { asked for copyright info }
  166.       end;  { one parameter passed }
  167.   end;  { function }
  168.  
  169.   function NumberToString (paramPtr: XCmdPtr;
  170.                   num: LONGINT): Str255;
  171. { use the toolbox call rather than HC's }
  172.     var
  173.       tempStr: str255;
  174.   begin
  175.     NumToString(num, tempStr);
  176.     NumberToString := tempStr;
  177.   end;
  178.  
  179.   procedure ReportVolError (paramPtr: XCmdPtr;
  180.                   errorNum: integer);
  181.     var
  182.       errMsg, tempName: str255;
  183.   begin
  184.     case errorNum of          { what caused the problem? }
  185.       bdNamErr: 
  186.         errMsg := 'Bad volume name.';
  187.       extFSErr: 
  188.         errMsg := 'External file system.';
  189.       ioErr: 
  190.         errMsg := 'I/O Error.';
  191.       nsDrvErr: 
  192.         errMsg := 'No such drive.';
  193.       nsvErr: 
  194.         errMsg := 'No such volume.';
  195.       paramErr: 
  196.         errMsg := 'No default volume.';
  197.       otherwise
  198.         errMsg := concat('unexpected error #', NumberToString(paramPtr, errorNum));
  199.     end;    { case }
  200.  
  201.     errMsg := concat('Sorry, ', errMsg);
  202.     reportToUser(paramPtr, errMsg);
  203.     { return the error message }
  204.   end;    { function }
  205.  
  206.   function validVolumeName (volumeName: str255): str255;
  207.   { a volume name must have one (and only one) colon in it, as }
  208.   { the last character }
  209.   begin
  210.     if pos(':', volumeName) = 0 then
  211.       validVolumeName := concat(volumeName, ':')
  212.     else
  213.       validVolumeName := copy(volumeName, 1, pos(':', volumeName));
  214.   end;
  215.  
  216.   procedure UnmountDisk (paramPtr: XCmdPtr);
  217.     var
  218.       getParamsOK: boolean;
  219.       volToUnmount: str255;
  220.       PB: ParamBlockRec;
  221.       errorCode: OSErr;
  222.   begin
  223.   { fetch and validate the passed parameters}
  224.     if AskedForHelp(paramPtr, 'UnmountVolume volumeName, ┬½ΓÇ£noDialog:ΓÇ¥errorGlobal┬╗', '┬⌐ 1990 Apple Computer, Inc. v.1.1,  bu Eric Carlson.') then
  225.       exit(UnmountDisk);
  226.  
  227.     if paramPtr^.paramCount = 0 then
  228.       begin
  229.         ReportToUser(paramPtr, 'Volume name expected.');
  230.         exit(UnmountDisk);
  231.       end
  232.     else
  233.       ZeroToPas(paramPtr, paramPtr^.Params[1]^, volToUnmount);
  234.  
  235.     volToUnmount := validVolumeName(volToUnmount);        { make sure the volume name is correct }
  236.  
  237.   { initialize parameter block.  Since volToUnmount is a full pathname, no other field is needed}
  238.     zeroBytes(paramPtr, @PB, sizeOf(PB));
  239.     PB.ioNamePtr := @volToUnmount;
  240.     PB.ioVolIndex := -1;
  241.     errorCode := PBGetVInfo(@PB, false);
  242.     if errorCode <> noErr then
  243.       begin
  244.         ReportVolError(paramPtr, errorCode);
  245.         exit(UnmountDisk);
  246.       end;
  247.  
  248.     errorCode := PBUnmountVol(@PB);
  249.     if errorCode <> noErr then
  250.       ReportVolError(paramPtr, errorCode);
  251.     exit(UnmountDisk);
  252.  
  253.   end;    { procedure UnmountDisk}
  254.  
  255.   procedure MAIN (paramPtr: XCmdPtr);
  256.   begin
  257.     UnmountDisk(paramPtr);
  258.   end;
  259.  
  260. end.  { unit UnmountDisk}